package com.sky.controller.admin;

import com.sky.annotation.AutoFill;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理相关接口")
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * @Description : 分页查询菜品数据
     *  * @param dishPageQueryDTO
     * @Return : com.sky.result.Result
     */
    @GetMapping("page")
    @ApiOperation("分页查询菜品数据")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询菜品数据");
        PageResult pageResult=dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * @Description : 新增菜品
     *  * @param dishDTO
     * @Return : com.sky.result.Result
     */
    @PostMapping
    @ApiOperation("新增菜品")
    @CacheEvict(cacheNames = "dishCache",key = "#dishDTO.categoryId")
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品");
        dishService.saveWithFlavor(dishDTO);
        return Result.success();
    }
    /**
     * @Description : 菜品删除
     *  * @param ids
     * @Return : com.sky.result.Result
     */
    @DeleteMapping
    @ApiOperation("菜品删除")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        log.info("菜品删除 {}",ids);
        dishService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * @Description : 根据id查询菜品
     *  * @param id
     * @Return : com.sky.result.Result<com.sky.vo.DishVO>
     */
    @GetMapping("{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据id查询菜品{}",id);
        DishVO dishVO=dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    /**
     * @Description : 修改菜品
     *  * @param dishDTO
     * @Return : com.sky.result.Result
     */
    @PutMapping
    @ApiOperation("修改菜品")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品{}",dishDTO);
        dishService.updateWithFlavor(dishDTO);
        return Result.success();
    }
    /**
     * @Description : 起售停售菜品
     *  * @param status
     * @param id
     * @Return : com.sky.result.Result
     */
    @PostMapping("/status/{status}")
    @ApiOperation("起售停售菜品")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result startOrStop(@PathVariable int status,Long id) {

        log.info("起售停售菜品");
        dishService.startOrStop(id, status);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("套餐中添加菜品时回显")
    public Result<List<Dish>> list(Long categoryId){

        log.info("套餐中添加菜品时回显");
        List<Dish> dishList = dishService.getDishesByCategoryId(categoryId);
        return Result.success(dishList);
    }

}
